package net.arvin.service;

import net.arvin.dao.ResponseHandler;
import net.arvin.utils.HttpRequestClient;
import net.arvin.view.LoadingView;

import org.apache.http.Header;

import android.content.Context;
import android.widget.Toast;

import com.loopj.android.http.TextHttpResponseHandler;

public class TransfersService {
	public static void tryGetAllTransfer(final String phonenumber,
			final ResponseHandler handler, final Context context) {
		HttpRequestClient.startPostGetTransferBillRequest(phonenumber,
				new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "未获取到数据=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody == null) {
							Toast.makeText(context, "服务器出错啦=_=!",
									Toast.LENGTH_SHORT).show();
						} else {
							if (responseBody.equals("")) {
								handler.onRequestFailed(responseBody);
							} else {
								handler.onRequsetSucceeded(responseBody);
							}
						}
					}
				}, context);
	}

	public static void tryTransferMoney(final String transfersJson,final String id,
			final String password, final ResponseHandler handler,
			final Context context) {
		showLoadingDialog(context, "转账中...");
		HttpRequestClient.startPostTransferMoneyRequest(transfersJson,id,
				password, new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
						closeLoadingDialog();
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody.equals("user")) {
							handler.onRequestFailed(responseBody);
						} else if (responseBody.equals("card")) {
							handler.onRequestFailed(responseBody);
						} else if (responseBody.equals("password")) {
							handler.onRequestFailed(responseBody);
						} else if (responseBody.equals("money")) {
							handler.onRequestFailed(responseBody);
						} else if (responseBody.equals("sql")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
						closeLoadingDialog();
					}
				}, context);

	}

	public static void tryCreatePayCode(final String codeInfo,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "生成中...");
		HttpRequestClient.startPostCreatePayCodeRequest(codeInfo,
				new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
						closeLoadingDialog();
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody.equals("")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
						closeLoadingDialog();
					}
				}, context);
	}

	public static void trySweepCode(final String url,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "获取中...");
		HttpRequestClient.startGetSweepCode(url, new TextHttpResponseHandler() {
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseBody, Throwable error) {
				if (responseBody == null) {
					Toast.makeText(context, "连接超时=_=!", Toast.LENGTH_SHORT)
							.show();
				}
				closeLoadingDialog();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseBody) {
				if (responseBody.equals("")) {
					handler.onRequestFailed(responseBody);
				} else {
					handler.onRequsetSucceeded(responseBody);
				}
				closeLoadingDialog();
			}
		}, context);
	}

	public static void showLoadingDialog(final Context context,
			final String message) {
		LoadingView.show(context, message);
	}

	public static void closeLoadingDialog() {
		LoadingView.dismiss();
	}
}
